﻿namespace Sitecore.ChangesTracker.Commands
{
  using System;
  using System.Collections.Generic;
  using System.Collections.Specialized;
  using System.Linq;
  using System.Text;
  using Sitecore.Diagnostics;
  using Sitecore.Shell.Framework.Commands;
  using Sitecore.Text;
  using Sitecore.Web.UI.Sheer;
  using Sitecore.Web.UI.WebControls;
  using Sitecore.Web.UI.XamlSharp.Continuations;
  using Sitecore.Web;
  using Sitecore.Data;
  using Sitecore.StringExtensions;
  using Sitecore.ChangesTracker.Archiving;
  using Sitecore.Data.Archiving;

  public class Compare : Command
  {
    public override void Execute(CommandContext context)
    {
      Assert.ArgumentNotNull(context, "context");
      string str = context.Parameters["item"].Split(new char[]{'|'})[0];
      string str2 = context.Parameters["archivename"];
      if (string.IsNullOrEmpty(str2))
      {
        str2 = "changetracker";
      }
      
        if (string.IsNullOrEmpty(str))
        {
          SheerResponse.Alert("Select an item first.", new string[0]);
        }
        else
        {
          ChangeTrackerArchiveEntry entry = ((ChangeTrackerArchive)((ChangeTrackerArchiveProvider)ArchiveManager.Providers["changetracker"]).GetArchive(str2, Sitecore.Context.ContentDatabase)).GetSingleEntry(new Guid(str));
          if (entry == null)
          {
              SheerResponse.Alert("Item not found.", new string[0]);
              return;
          }
          if (Sitecore.Context.ContentDatabase.GetItem(entry.ItemId) == null)
          {
            SheerResponse.Alert("There is no item to compare in database {0}.".FormatWith(new object[] { Sitecore.Context.ContentDatabase.Name }), new string[0]);
          }
          else
          {
              if (!entry.IsTemplate)
              {
                  UrlHandle handle = new UrlHandle();
                  UrlString parameters = new UrlString();
                  parameters.Append("hdl", handle.Handle);
                  parameters.Append("arc_id", str);
                  parameters.Append("archivename", str2);
                  WebUtil.RemoveSessionValue("arc_ver_db");
                  WebUtil.RemoveSessionValue("arc_ver_cp");
                  WebUtil.RemoveSessionValue("arc_lang_cp");
                  WebUtil.RemoveSessionValue("arc_lang_db");
                  Sitecore.Shell.Framework.Windows.RunApplication("/sitecore/content/Applications/Archives/Compare Items", parameters.ToString());
              }
              else
              {
                  UrlString url = new UrlString(UIUtil.GetUri("control:Sitecore.ChangesTracker.UI.TemplateComparer"));
                  url.Add("arcid", str);
                  url.Add("arcName", str2);
                  SheerResponse.ShowModalDialog(url.ToString(),"1100","500",string.Empty, false);
                  SheerResponse.SetLocation(string.Empty);
              }
        }
      }
    }
    //public override CommandState QueryState(CommandContext context)
    //{
    //  Assert.ArgumentNotNull(context, "context");
    //  string str = context.Parameters["item"];
    //  if (string.IsNullOrEmpty(str))
    //  {
    //    return CommandState.Disabled;
    //  }
    //  else
    //  {
    //    return base.QueryState(context);
    //  }
    //}
  }
}
